<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<style>
    * {
        margin: 0;
        padding: 0;
    }

    .box {
        width: 200px;
        height: 100px;
        background-color: red;
        overflow-x: scroll;
        scroll-behavior: smooth;
    }

    .longbox {
        width: 800px;
        height: 100px;
        position: relative;
    }

    .longbox span {
        cursor: pointer;
        margin: 0 50px 0 0;
    }

    .spancolor {
        color: blue;
    }

    .box2 {
        overflow: scroll;
        height: 200px;
        width: 200px;
        background-color: red;
        position: relative;
    }

    .box2 p {
        margin: 0 0 200px 0;
    }

    #line {
        position: absolute;
        background-color: aqua;
        width: 38px;
        height: 5px;
        transition: all 0.2s;
    }
</style>

<body>
    <div id="app">
        <!--  -->
        <div class="box" id="scroll1">
            <div class="longbox">
                <span v-bind:class='{spancolor:num==index}' v-for="item,index in biaoqian" @click="fn(index)"
                    ref="span1">{{item}}</span>
                <div id="line"></div>
            </div>
        </div>
        <div class="box2" id="scroll2" @scroll="gundong">
            <p v-for="item in biaoqian" ref="p1">{{item}}</p>
        </div>
        <!--  -->
    </div>
</body>
<script src="../../static/vue.js"></script>
<script>
  new Vue({
    el: "#app",
    data: {
      num: 0,
      biaoqian: ['标签1', '标签2', '标签3', '标签4', '标签5', '标签6', '标签7', '标签8']
    },
    methods: {
      fn(index) {
        this.num = index
        scroll1.scrollLeft = this.$refs.span1[index].offsetLeft - scroll1.offsetWidth / 3;
        let juli = this.$refs.span1[index].offsetLeft
        line.style.transform = `translateX(${juli}px)`;
        scroll2.scrollTop = this.$refs.p1[index].offsetTop - scroll2.offsetHeight / 2
      },
      gundong(){
        let str = scroll2.scrollTop   
        this.$refs.p1.forEach((v, i) => {
          if (str-v.offsetTop>=10&&str-v.offsetTop<100) {
            this.num = i
            scroll1.scrollLeft = this.$refs.span1[i].offsetLeft - scroll1.offsetWidth / 3
            let juli = this.$refs.span1[i].offsetLeft
            line.style.transform = `translateX(${juli}px)`
            scroll2.scrollTop = this.$refs.p1[i].offsetTop - scroll2.offsetHeight / 2 
          }
        });
      }
    },
  })
</script>

</html>